package cn.itsource.system.service.impl;

import cn.itsource.system.domain.Role;
import cn.itsource.system.mapper.RoleMapper;
import cn.itsource.system.service.IRoleService;
import cn.itsource.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hhm
 * @since 2023-04-01
 */
@Service
public class RoleServiceImpl extends BaseServiceImpl<Role> implements IRoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    @Transactional
    public boolean add(Role role) {
        //保存role
        boolean add = super.add(role);
        //保存关联表
        initRelation(role);

        return add;
    }

    private void initRelation(Role role) {
        //删除role的menu关联信息
        roleMapper.deleteRoleMenu(role.getId());
        //删除role的permission关联信息
        roleMapper.deleteRolePermission(role.getId());
        //保存role_menu
        roleMapper.saveRoleMenu(role.getId(), role.getMenus());
        //保存role_permission
        roleMapper.saveRolePermission(role.getId(), role.getPermissions());
    }


}
